<?php 
include('../../Connections/KRAPPERCON.php');
$oper  = $_POST['oper'];
$cldoc = getVal('cbo_tr_cldoc');
$origen = getVal('cbo_tr_ori');
$destino = getVal('cbo_tr_des');
// $ref = $_POST['t_tr_ref'];
// Inicialmente siempre grabar la guia en blanco
$ref = '';
$txt = getVal('t_tr_txt');
$items = getVal('items');

if ($oper == 'upd'){
$sql = "call sp_doMigo('".$cldoc."','". $user_now ."','". $ref ."','". $txt ."','". $origen."','". $destino ."','". $items ."');";
doEcho($sql);
$res = $mysqli->query($sql);
if ($res){
	if ($row = $res->fetch_assoc()){
		if ($row['newnro'] > 0 ){
		echo "<script type='text/javascript'>GEBI('a_nroMov').innerHTML = '".$row['newnro']."'; showOk('Se ha registrado el Movimiento de Stocks ".$row['newnro']." correctamente');</script>";
		}
		else {
		echo "<script type='text/javascript'>showError('".$row['msg_err']." ".$row['pos_doc']."');</script>";
		}
	}
$mysqli->check_result();
}
/* 
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_doMigo`(cladoc char(5) , usrid char(15),
ref varchar(20), origen int, dest int, items varchar(1000))
*/
}
elseif ($oper == 'guia'){
/*
oper : 'guia' ,
		movimiento : doc,
		ClientID : GEBI('t_gu_cdc').value,
		ClientNIF : GEBI('t_gu_nif').value,
		ClientNom : GEBI('t_gu_raz').value,
		ClientDir : GEBI('t_gu_dir').value,
		texto : GEBI('t_gu_txt').value
		*/
	$mov =getVal('movimiento');
	$idcli = trim(getVal('ClientID'));
	$destino = getVal('Destino');
	$partida = getVal('Partida');
	
	
	if ($idcli == '[Auto]') $idcli = 0;
	$nfcli = getVal('ClientNIF');
	$nocli = getVal('ClientNom');
	$dircli = getVal('ClientDir');
	$texto = getVal('texto');
	
	$sql = "call sp_doGuiaMov('".$mov."','".$user_now."','".$idcli."','".$nfcli."','".$nocli."','".$dircli."','".$texto."','".$partida."','".$destino."')";
	echo($sql);
	$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			$guia = trim($row['guia']);
			if ($guia != '') {
			?>
				<script type='text/javascript'>
				closePopup();
				showOk('Se ha generado correctamente la Guia : <?php echo $guia;?>');
				if(confirm('Desea proceder con la impresion?')) {
				window.open('Prints/printGuiaRem.php?oper=print&clase=MOV&factura=<?php echo $mov;?>&guia=<?php echo $guia;?>');
				}
				closePopup();
                </script>;
               <?php 
			}
			else {
				echo '<script type="text/javascript">showError("'.$row['msg_err'].'");</script>';
			}
		}
	$res->close();
	$mysqli->check_result();
	}



}

?>